![]() |
PATH![]() |
![]() ![]() |
Here's a subroutine, called minimumValue , that returns the smaller of two values:
-- minimumValue subroutine:
on minimumValue(x, y)
if x < y then
return x
else
return y
end if
end minimumValue
-- To call minimumValue:
minimumValue(5, 105)
The first line of the minimumValue subroutine specifies the parameters of the subroutine. These can be positional parameters--like x and y in the example--where the order of the parameters is significant, or labeled parameters--like those for many of the AppleScript and application commands described in Commands --where the order of parameters other than the direct parameter doesn't matter.
The minimumValue subroutine includes two Return statements. A Return statement is one of the ways a subroutine can return a result. When AppleScript executes a Return statement, it returns the value (if any) listed in the statement and immediately exits the subroutine. If AppleScript executes a Return statement without a value, it exits the subroutine immediately and does not return a value.
If a subroutine does not include any Return statement, AppleScript executes the statements in the subroutine and, after handling the last statement, returns the value of the last statement in the subroutine. If the last statement does not return a value, then the subroutine does not return a value.
When AppleScript has finished executing a subroutine, it passes control to the place in the script immediately after the place where the subroutine was called. If a subroutine call is part of an expression, AppleScript uses the value returned by the subroutine to evaluate the expression. For example, to evaluate the following expression, AppleScript calls the subroutine for minimumValue , then evaluates the rest of the expression.
minimumValue(5, 105) + 50 --result: 55
For related information, see Using Results and The Return Statement.